
local function Fn()
    local Fx = Class()
    local function GetItem_Base_prefab_name(item)
        return item.nameoverride or item.prefab
    end
    local function GetItemNum(item)
        local ret = 1
        if item.components.stackable then
            ret = item.components.stackable.stacksize
        end
        return ret
    end

    function Fx:GetFoods(inst)
        local cooked_food_list = inst.npc_base_lib:Get_Cooked_Food_List()
        local ret_list = {}
        local ret_num = 4
        for i = 1, 100, 1 do        ------- 找4个不同的食物
            local temp_name = cooked_food_list[math.random(#cooked_food_list)]
            if ret_list[temp_name] == nil then
                ret_list[temp_name] = 1
                ret_num = ret_num - 1
                if ret_num <= 0 then
                    break
                end
            end
        end

        local temp_table = {}
        for name, num in pairs(ret_list) do
            table.insert(temp_table,{name = name , num = num})
        end
        return temp_table
    end

    function Fx:ItemReadyCheck(inst)
        -----------------------------------------------------------------------
        --- 安全检查
            if inst == nil then
                return
            end
            local foods = inst.npc_everything_data:Get("_item_ask_table")
            if foods == nil or type(foods) ~= "table" or #foods ~= 4 then
                return
            end
        -----------------------------------------------------------------------
        ------ 转换一下
        local foods_ask_table = {}
        for k, v in pairs(foods) do
            if v and type(v) == "table" and v.name and v.num then
                foods_ask_table[v.name] = v.num
            end
        end
        -- print("+++++++++++++++++++++++++++")
        -- for k, v in pairs(foods_ask_table) do
        --     print(k,v)
        -- end
        -- print("+++++++++++++++++++++++++++")
        if inst.components.container:IsEmpty() then
            return false
        end

        local all_items = inst.components.container:GetAllItems()
        for k, item in pairs(all_items) do
            if item and item.prefab then
                local prefab_name = GetItem_Base_prefab_name(item)
                local ask_num = foods_ask_table[prefab_name]
                local current_num = GetItemNum(item)
                -- print(prefab_name,ask_num,current_num)
                if current_num >= ask_num then
                    foods_ask_table[prefab_name] = true
                end 
            end
        end

        for k, v in pairs(foods_ask_table) do
            if v ~= true then
                return  false
            end
        end
        return true
    end

    function Fx:Buyer_Appear(inst,doer)        
        -----------------------------------------------------------------------
        ----- 检查通过，开始计算数据
        inst:AddTag("INLIMBO")
        inst:AddTag("notarget")
        inst:AddTag("noattack")
        inst:AddTag("invisible")
        inst:AddTag("no_announce")

        local all_items = inst.components.container:GetAllItems()
        inst.npc_everything_data:Set("type","dining_table")
        inst.npc_everything_data:Set("reward_name","goldnugget")
        inst.npc_everything_data:Add("reward_num",5)    --------- 基础餐桌价格
        for k, item in pairs(all_items) do
            local num = GetItemNum(item)
            local base_gold_num_each = math.random(10,18)
            if GetItem_Base_prefab_name(item) ~= item.prefab then   --- 如果加了调料, 1.5 倍价格
                base_gold_num_each = math.ceil(base_gold_num_each*1.5)
            end
            inst.npc_everything_data:Add("reward_num",base_gold_num_each*num)
        end

        if TUNING.NPC_CONFIG.REWARDS_LESS ~= true then  --- 奖励1.5倍
            local reward_num = inst.npc_everything_data:Add("reward_num",0)
            inst.npc_everything_data:Add("reward_num",math.ceil(reward_num/2))
        end
        -----------------------------------------------------------------------
        -- 数据计算完成，开始生成npc
        inst.npc_base_lib:For_Sale_Immediately()
        -----------------------------------------------------------------------
    end

    return Fx
end

return Fn